package com.scms.secondclass.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.scms.common.utils.DateUtils;
import com.scms.common.utils.SecurityUtils;
import com.scms.secondclass.domain.Teacher;
import com.scms.secondclass.domain.TeacherRole;
import com.scms.secondclass.mapper.TeacherMapper;
import com.scms.secondclass.mapper.TeacherRoleMapper;
import com.scms.secondclass.service.ITeacherService;

/**
 * 教师信息Service业务层处理
 * 
 * @author scms
 */
@Service
public class TeacherServiceImpl implements ITeacherService 
{
    @Autowired
    private TeacherMapper teacherMapper;

    @Autowired
    private TeacherRoleMapper teacherRoleMapper;

    /**
     * 查询教师信息
     * 
     * @param teacherId 教师信息主键
     * @return 教师信息
     */
    @Override
    public Teacher selectTeacherByTeacherId(Long teacherId)
    {
        return teacherMapper.selectTeacherByTeacherId(teacherId);
    }

    /**
     * 查询教师信息列表
     * 
     * @param teacher 教师信息
     * @return 教师信息
     */
    @Override
    public List<Teacher> selectTeacherList(Teacher teacher)
    {
        return teacherMapper.selectTeacherList(teacher);
    }

    /**
     * 新增教师信息
     * 
     * @param teacher 教师信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertTeacher(Teacher teacher)
    {
        teacher.setCreateTime(DateUtils.getNowDate());
        teacher.setCreateBy(SecurityUtils.getUsername());
        int rows = teacherMapper.insertTeacher(teacher);
        insertTeacherRole(teacher);
        return rows;
    }

    /**
     * 修改教师信息
     * 
     * @param teacher 教师信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateTeacher(Teacher teacher)
    {
        teacher.setUpdateTime(DateUtils.getNowDate());
        teacher.setUpdateBy(SecurityUtils.getUsername());
        
        // 删除用户与角色关联
        teacherRoleMapper.deleteUserRoleByUserId(teacher.getUserId());
        // 新增用户与角色关联
        insertTeacherRole(teacher);
        
        return teacherMapper.updateTeacher(teacher);
    }

    /**
     * 批量删除教师信息
     * 
     * @param teacherIds 需要删除的教师信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTeacherByTeacherIds(Long[] teacherIds)
    {
        // 删除用户与角色关联
        for (Long teacherId : teacherIds)
        {
            Teacher teacher = teacherMapper.selectTeacherByTeacherId(teacherId);
            if (teacher != null && teacher.getUserId() != null)
            {
                teacherRoleMapper.deleteUserRoleByUserId(teacher.getUserId());
            }
        }
        return teacherMapper.deleteTeacherByTeacherIds(teacherIds);
    }

    /**
     * 删除教师信息信息
     * 
     * @param teacherId 教师信息主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteTeacherByTeacherId(Long teacherId)
    {
        Teacher teacher = teacherMapper.selectTeacherByTeacherId(teacherId);
        if (teacher != null && teacher.getUserId() != null)
        {
            teacherRoleMapper.deleteUserRoleByUserId(teacher.getUserId());
        }
        return teacherMapper.deleteTeacherByTeacherId(teacherId);
    }
    
    /**
     * 新增教师角色信息
     * 
     * @param teacher 教师对象
     */
    public void insertTeacherRole(Teacher teacher)
    {
        Long roleId = teacher.getRoleId();
        if (roleId != null)
        {
            // 新增用户与角色管理
            TeacherRole ur = new TeacherRole();
            ur.setUserId(teacher.getUserId());
            ur.setRoleId(roleId);
            teacherRoleMapper.insertUserRole(ur);
        }
    }
} 